<template>
  <div class="teacher-edit-page">
    <el-card>
      <div class="edit-title">新增教师</div>
      <el-form :model="form" :rules="rules" ref="formRef" label-width="120px" class="teacher-form">
        <div class="form-row">
          <el-form-item label="教师姓名：" prop="name">
            <el-input v-model="form.name" placeholder="教师姓名" />
          </el-form-item>
          <el-form-item label="联系电话：" prop="contactPhone">
            <el-input v-model="form.contactPhone" placeholder="联系电话" />
          </el-form-item>
        </div>
        <div class="form-row">
          <el-form-item label="所授课程：" prop="coursesTaught">
            <el-input v-model="form.coursesTaught" placeholder="如：语文,数学" />
          </el-form-item>
          <el-form-item label="职称：" prop="professionalTitle">
            <el-input v-model="form.professionalTitle" placeholder="职称" />
          </el-form-item>
        </div>
        <div class="form-actions">
          <el-button type="primary" @click="handleSubmit">确定</el-button>
          <el-button @click="goBack">取消</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { addTeacher } from '@/api/teacher';

const router = useRouter();
const formRef = ref();
const form = reactive({
  name: '',
  coursesTaught: '',
  professionalTitle: '',
  contactPhone: '',
});

const rules = {
  name: [{ required: true, message: '请输入教师姓名', trigger: 'blur' }],
  coursesTaught: [{ required: true, message: '请输入所授课程', trigger: 'blur' }],
  professionalTitle: [{ required: true, message: '请输入职称', trigger: 'blur' }],
  contactPhone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
};

function handleSubmit() {
  formRef.value.validate((valid: boolean) => {
    if (valid) {
      addTeacher(form).then(() => {
        ElMessage.success('新增成功');
        goBack();
      });
    }
  });
}

function goBack() {
  router.back();
}
</script>

<style scoped>
.teacher-edit-page {
  padding: 40px 24px;
}
.edit-title {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 24px;
}
.teacher-form {
  background: #fafbfc;
  padding: 24px 16px 8px 16px;
  border-radius: 8px;
}
.form-row {
  display: flex;
  gap: 24px;
}
.form-actions {
  margin-top: 24px;
  text-align: left;
}
</style> 